/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.process;

import org.openaecfm.model.datetime.TaskTime;
import org.openaecfm.model.kernel.Object;
import org.openaecfm.model.process.Task;
import org.openaecfm.model.process.TaskTypeEnum;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.IntegerParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class TaskIO extends AbstractEntityInstanceInputImpl<Task>
		implements IEntityInstanceUtilInput<Task>, IEntityInstanceOutput<Task>,
		IEntityInstanceUtilOutput<Task> {
	private IEntityInstanceUtilInput<Object> utilInput;
	private IEntityInstanceUtilOutput<Object> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Object> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Task pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCTASK",
				appDataManager.getName(pojo));

		exportSPF(pojo, ei, appDataManager);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Task doCreateObject() {
		return new Task();
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<org.openaecfm.model.kernel.Object> utilInput) {
		this.utilInput = utilInput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(Task pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set Status
		if (pojo.getStatus() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getStatus(),
					StringParameter.UTF_8));
		}

		// Set WorkMethod
		if (pojo.getWorkMethod() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getWorkMethod(),
					StringParameter.UTF_8));
		}

		// Set IsMilestone
		sei.addParameter(new EnumerationParameter(pojo.getIsMilestone()));

		// Set Priority
		if (pojo.getPriority() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new IntegerParameter(pojo.getPriority()));
		}

		// Set TaskTime
		if (pojo.getTaskTime() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getTaskTime())));
		}

		// Set PredefinedType
		if (pojo.getPredefinedType() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EnumerationParameter(pojo.getPredefinedType()));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei, Task pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Status
		Parameter status = sei.getParameter(7);
		if (status.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) status;
			pojo.setStatus(s.toUTF8());
		}

		// Set WorkMethod
		Parameter method = sei.getParameter(8);
		if (method.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) method;
			pojo.setWorkMethod(s.toUTF8());
		}

		// Set IsMilestone
		EnumerationParameter milestone = (EnumerationParameter) sei
				.getParameter(9);
		pojo.setIsMilestone(org.openaecfm.model.measure.Boolean
				.valueOf(milestone.toEnumString()));

		// Set Priority
		Parameter pri = sei.getParameter(9);
		if (pri.getNodeType() == NodeType.INTEGER) {
			IntegerParameter i = (IntegerParameter) pri;
			pojo.setPriority(i.toInteger());
		}

		// Set PredefinedType
		Parameter predefinedType = sei.getParameter(11);
		if (predefinedType.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) predefinedType;
			pojo.setPredefinedType(TaskTypeEnum.valueOf(e.toEnumString()));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Task pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TaskTime
		Parameter taskTime = sei.getParameter(10);
		if (taskTime.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) taskTime;
			pojo.setTaskTime((TaskTime) appDataManager.getObject(e
					.toInstanceName()));
		}
	}
}
